define(['jquery','api','user','util','text!pages/fund/charge.html','Vue'],function($,api,user,util){
	var vm,
	   backurl,
	   statusHash={},
	   channels=[],
       userfunds={balance:'--'},
       banks={},//银行列表
       channels={},//通道列表
       userchannels={},
       userchannelsTimer,
       banksTimer,
       channelsTimer,
	   controller = {
	   init:function(url){
	   	   if(!user.checkLogin())return;
		   backurl=url;
		   	api.headerShow(1);
			api.headerTitle(L('html_title_fund_charge'));
		   api.footerShow(0);//隐藏底部导航
		   api.setTitle(L('html_title_fund_charge'));
		   
		   	this.render();
	   },
	   render:function(){
	   		var that=this;
	   		$('#common').html('<div id="charge"></div>');
	       require(['text!pages/fund/charge.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#charge',
	               data:{
	               	  show:true,
	               	  loading:false,
	               	  userfunds:userfunds,
	                  channels:channels,
	                  inChannel:'',
	                  bankInfo:[],
	                  inMoney:100,
	                  otherMoney:false,
	                  moneyType:[100, 500, 1000, 5000]
	               },
	               filters:{
	                  toStr:function(n){
	                 	 return String.fromCharCode(n);
	                  },
	                   priceFormat:function(v){
	                      if(isNaN(v)){return v='--'}; // 20161209dyx
	                      return util.priceFormat(2,v);
	                   }
	               },
	               methods:{
	                  setMoney:function(n){
		                	this.inMoney=n;
		                	this.otherMoney=n=='';
		                },
		                toCharge:function(){
		                	var data={
		                	   amount:this.inMoney * 100,
		                	   channel:this.inChannel,
		                	   type:51
		                	};

		                	if(this.inMoney==''||+this.inMoney<=0){
		                		msg('请选择或输入金额！');
		                	}else{
		                       api.charge(data,function(rs){
		                           if(rs.retcode=='0' && rs.inoutfund){
		                           		if(rs.inoutfund.urlcontent){
			                              location.href=rs.inoutfund.urlcontent+'&backurl='+backurl;
		                           		}else{
			                           		util.msg('充值提交成功');
		                           		}
		                           }else{
		                           		util.dealErrorCode(rs.retcode);
		                           }
		                       });
		                	}
		                }
	               },
	               template:tpl
	            })
	            
		       vm.loading=true;
		       that.getUserInfo();
	       });
	       
	   },
	   initChannel:function(){ // 基于用户通道获取银行通道
	   		vm.loading=false;
	   	   var 
	   	       channelHash={},
	   	       channel,
	   	       channelid,
	   	       userchannel,
	   	       temp=[],
	   	       cb=function(){
	   	       	  for(var i=0;i<userchannels.length;i++){
	   	       	  	   channel=userchannels[i];
	   	       	  	   if (channel.isprimary==='49'){
		   	       	       channelHash[channel.channelid]=channel;
	   	       	  	   }
	   	       	  }
	   	       	  
	   	       	  for(var i=0;i<channels.length;i++){
	   	       	  	   channel=channels[i];
	   	       	       if(+channel.inmode>50 || channel.channelid==48){
		   	       	  	  channelid=channel.channelid;
		   	       	  	  userchannel=channelHash[channelid];
	   	       	       	
		   	       	  	  if(userchannel){
		   	       	  	  	  channel["bankid"]=userchannel.bankid;
	                      	  channel.bankaccount=userchannel.bankaccount;
	                      	  channel.bank=userchannel.bankid&&banks[userchannel.bankid] ? banks[userchannel.bankid] : ''; 
                      			statusHash[channelid]=channel.bankaccount?0:1;
	                      	  temp.push(channel);
		   	       	  	  }
	   	       	       	  
	   	       	       }
	   	       	  }
	           	  vm.channels=temp;
	   	      };
	   	      
   	      if(banks && userchannels && channels)cb();
	   },
	   getUserInfo:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getUserInfo();
	   			};
	   			
   			api.userInfo(
   				function(rs){
			   	   if(rs.retcode=='0'){
			   	   		vm.userfunds=rs.data;
			   	   		userchannels=rs.data.channel?rs.data.channel:[];
			   	   		that.getBanks();
			   	   }else{
			   	      util.msg('用户未登录或超时！');
			   	      userchannelsTimer = setTimeout(cb,2000);
			   	   }
			   },
			   function(){//err
			   		userchannelsTimer = setTimeout(cb,2000);
			   }
			);
	   },
	   getBanks:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getBanks();
	   			};
	   		// 获取银行列表
       	  	api.banks(
       	  		function(rs){
				   if(rs.retcode=='0' && rs.data){
					banks = rs.data;
					that.getChannels();
				   }else if(rs.retcode=='43'){
				       util.msg('系统缓存服务bug,请联系管理员！')
				       banksTimer = setTimeout(cb,2000);
				   }else{
				   		banksTimer = setTimeout(cb,2000);
				   }
				},
				function(){//err
					banksTimer = setTimeout(cb,2000);
				}
   	  		);
	   },
	   getChannels:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getChannels();
	   			};
	   			
	   		api.channels(
	   			function(rs){
		           if(rs.retcode=='0' && rs.data){
		           	  channels=rs.data;
		           	  that.initChannel();
		           }else{
		           		channelsTimer = setTimeout(cb,2000);
		           }
		       },
		       function(){//err
		       		channelsTimer = setTimeout(cb,2000);
		       }
   			);
	   },
	   cleartFullTimer:function(){
		   	if(channelsTimer) clearTimeout(channelsTimer);
		   	if(banksTimer) clearTimeout(banksTimer);
		   	if(channelsTimer) clearTimeout(channelsTimer);
		   	if(userchannelsTimer) clearTimeout(userchannelsTimer);
		   	if(allowFundTimer) clearTimeout(allowFundTimer);
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})